home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / OpenInventorLab / labSolutions / walk4.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.3 KB  |  155 lines

  1. //-----------------------------------------------------------------------
  2. //
  3. // SOLUTION: Inventor Barcelona Lab #4
  4. //
  5. //    This program illustrates a simple Inventor program which reads
  6. //    in an Inventor file and views it with the `Walkthrough viewer'.
  7. //    If the `lamp' is picked then play some audio and turn the light on/off...
  8. //
  9. //    EXERCISE:
  10. //    When the `Table' is picked, spin the `Lamp' about the center
  11. //      of the table.
  12. //
  13. //    Hint: Find the SoRotor `TableRotor' in scene.iv and turn it on
  14. //          when the table is picked...
  15. //
  16. //-----------------------------------------------------------------------
  17.  
  18. #include <stdio.h>
  19. #include <Inventor/SoDB.h>
  20. #include <Inventor/actions/SoSearchAction.h>
  21. #include <Inventor/events/SoMouseButtonEvent.h>
  22. #include <Inventor/nodes/SoEventCallback.h>
  23. #include <Inventor/nodes/SoSeparator.h>
  24. #include <Inventor/nodes/SoSwitch.h>
  25. #include <Inventor/nodes/SoRotor.h>
  26. #include <Inventor/Xt/SoXt.h>
  27. #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
  28.  
  29.  
  30. SoRotor  *tableRotor;
  31. SoSwitch *lampSwitch;
  32.  
  33. void lampCallback( void *, SoEventCallback *cb ) {
  34.     // Make sure that this is a MOUSE PRESS event
  35.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  36.  
  37.     if ( lampSwitch->whichChild.getValue() == SO_SWITCH_NONE ) {
  38.     // Lamp is OFF - turn it ON...
  39.     lampSwitch->whichChild = 0;
  40.     system( "playaiff sounds/lampON.aiff &" );
  41.     }
  42.     else {
  43.     // Lamp is ON - turn it OFF...
  44.     lampSwitch->whichChild = SO_SWITCH_NONE;
  45.     system( "playaiff sounds/lampOFF.aiff &" );
  46.     }
  47. }
  48.  
  49. void lampSwitchInit( SoSeparator *root )
  50.     // Find the `Lamp' and the `Lamp Switch' nods.
  51.     SoGroup *lamp = (SoGroup *) root->getByName( "Lamp" );
  52.     lampSwitch = (SoSwitch *) root->getByName( "LampSwitch" );
  53.  
  54.     if ( (lamp == NULL) || (lampSwitch == NULL)
  55.         || (!lamp->isOfType( SoGroup::getClassTypeId()))
  56.         || (!lampSwitch->isOfType( SoSwitch::getClassTypeId())) ) {
  57.     printf( "DEBUG: `Lamp' or `LampSwitch' not found, or wrong types\n" );
  58.     exit( 0 );
  59.     }
  60.  
  61.     // Get the path to `Lamp'
  62.     SoSearchAction sa;
  63.     sa.setFind( SoSearchAction::NODE );
  64.     sa.setNode( lamp );
  65.     sa.apply( root );
  66.     SoPath *path = sa.getPath();
  67.  
  68.     // Create an event callback node that calls a function if the
  69.     // `Lamp' is picked.
  70.  
  71.     SoEventCallback *lampCB = new SoEventCallback;
  72.     lampCB->setPath(path);
  73.     lampCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  74.                 lampCallback );
  75.     lamp->addChild( lampCB );
  76. }
  77.  
  78.  
  79. void tableCallback( void *, SoEventCallback *cb ) {
  80.     // Make sure that this is a MOUSE PRESS event
  81.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  82.  
  83.     if ( tableRotor->on.getValue() ) {
  84.     system( "playaiff sounds/tableOFF.aiff &" );
  85.     tableRotor->on = FALSE;
  86.     }
  87.     else {
  88.     system( "playaiff sounds/tableON.aiff &" );
  89.     tableRotor->on = TRUE;
  90.     }
  91. }
  92.  
  93.  
  94. void tableInit( SoSeparator *root )
  95.     // Find the `Table' node
  96.     SoGroup *table = (SoGroup *) root->getByName( "Table" );
  97.     tableRotor = (SoRotor *) root->getByName( "TableRotor" );
  98.  
  99.     if ( (table == NULL) || (!table->isOfType( SoGroup::getClassTypeId())) ) {
  100.     printf( "DEBUG: `Table' not found or wrong type\n" );
  101.     exit( 0 );
  102.     }
  103.  
  104.     // Get the path to `Table'
  105.     SoSearchAction sa;
  106.     sa.setFind( SoSearchAction::NODE );
  107.     sa.setNode( table );
  108.     sa.apply( root );
  109.     SoPath *path = sa.getPath();
  110.  
  111.     // Create an event callback node that calls a function if the
  112.     // `Table' is picked.
  113.  
  114.     SoEventCallback *tableCB = new SoEventCallback;
  115.     tableCB->setPath(path);
  116.     tableCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  117.                 tableCallback );
  118.     table->addChild( tableCB );
  119. }
  120.  
  121.  
  122. main(int argc, char **argv)
  123. {
  124.     // Initialize Inventor
  125.     Widget myWindow = SoXt::init( argv[0] );
  126.     if (myWindow == NULL) exit(1);
  127.  
  128.     // Make a viewer part of the window
  129.     SoXtWalkViewer *viewer = new SoXtWalkViewer(myWindow);
  130.  
  131.     // Read the object from a file
  132.     SoInput myInput;
  133.     if (!myInput.openFile("./scene.iv")) return(1);
  134.     SoSeparator *scene = SoDB::readAll(&myInput);
  135.     if (scene == NULL) {
  136.     printf( "Error: scene.iv file read failed!\n" );
  137.     exit(1);
  138.     }
  139.     scene->ref();
  140.  
  141.     // Search for the different objects by name
  142.     lampSwitchInit( scene );
  143.     tableInit( scene );
  144.  
  145.     // Viewer setup
  146.     viewer->setSceneGraph( scene );
  147.     viewer->setTitle( "Walkthrough Program" );
  148.     viewer->show();
  149.     SoXt::show(myWindow);
  150.  
  151.     SoXt::mainLoop();
  152. }
  153.